﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;

namespace GUI.TimKiem
{
    public partial class frmTimKiemKhachHang : Form
    {
        bool checkClose = false;
        public static Entities.KhachHang khachhang;
        Entities.KhachHang[] hienthi;
        public frmTimKiemKhachHang()
        {
            InitializeComponent();
            rdbTimKiem1.Checked = true;
            dgvHienThi.DataSource = HienThi();
            hienthi = (Entities.KhachHang[])dgvHienThi.DataSource;
            fix();
            dgvHienThi.Focus();
        }

        public Entities.KhachHang[] HienThi()
        {
            Entities.CaiDatKho kho = Entitystatic.ViewFile();

            Entities.ChiTietCongNoKH a = new Entities.ChiTietCongNoKH();
            a.MaKhachHang = "";
            a.HanhDong = "0";
            a.MaKho = "";
            Entities.KhachHang kh = new Entities.KhachHang();
            kh.HanhDong = "1";
            Entities.ChiTietCongNoKH[] chitiet = Common.StaticVariable.InterfaceObject.SelectChiTietCongNoKH(a);

            if (chitiet == null)
                chitiet = new Entities.ChiTietCongNoKH[0];
            ArrayList array = new ArrayList();
            Entities.KhachHang[] temp = Common.StaticVariable.InterfaceObject.SelectKH(kh);

            foreach (Entities.KhachHang item in temp)
            {
                if (item.Deleted == false)
                {
                    foreach (Entities.ChiTietCongNoKH item1 in chitiet)
                    {
                        if (kho.MaKho != null && kho.MaKho == item1.MaKho && item1.MaKhachHang == item.MaKhachHang)
                        {
                            item.DuNo = item1.DuNo;
                            break;
                        }
                    }
                    array.Add(item);
                }
            }
            Entities.KhachHang[] khachhang = (Entities.KhachHang[])array.ToArray(typeof(Entities.KhachHang));
            return khachhang;
        }

        void fix()
        {
            for (int i = 0; i < dgvHienThi.ColumnCount; i++)
            {
                dgvHienThi.Columns[i].Visible = false;
            }
            dgvHienThi.Columns["MaKhachHang"].HeaderText = "Mã Khách Hàng";
            dgvHienThi.Columns["MaKhachHang"].Visible = true;
            dgvHienThi.Columns["Ten"].HeaderText = "Tên Khách Hàng";
            dgvHienThi.Columns["Ten"].Visible = true;
            dgvHienThi.Columns["DiaChi"].HeaderText = "Địa Chỉ";
            dgvHienThi.Columns["DiaChi"].Visible = true;
            if (dgvHienThi.RowCount > 0)
            {
                Common.Utilities.CountDatagridview(dgvHienThi);
                dgvHienThi.Focus();
                dgvHienThi.Select();
            }
            dgvHienThi.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            dgvHienThi.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;


        }

        #region Chọn
        void Chon()
        {
            try
            {
                if (khachhang == null)
                {
                    MessageBox.Show(Common.MSG.MSG20(), Common.MSG.TieuDe, MessageBoxButtons.OK);
                    return;
                }
                checkClose = true;
                this.Close();
            }
            catch
            {
            }
        }
        #endregion

        #region Thoát
        void Thoat()
        {
            try
            {
                if (MessageBox.Show(Common.MSG.MSG2(), Common.MSG.TieuDe, MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    checkClose = true;
                    this.Close();
                }
            }
            catch
            {
            }
        }
        #endregion

        private void Window_Loaded(object sender, EventArgs e)
        {
            this.Closing += new System.ComponentModel.CancelEventHandler(frmTimKiemKhachHang_Closing);
            // click chuột trái image
            btnThoat.MouseDown += new MouseEventHandler(MouseDown_Chung);
            btnChon.MouseDown += new MouseEventHandler(MouseDown_Chung);
            // text changed
            txtTimKiem.TextChanged += new EventHandler(txtTimKiem_TextChanged);
            // key up

            dgvHienThi.CellClick += new DataGridViewCellEventHandler(dgvHienThi_CellClick);
            dgvHienThi.CellDoubleClick += new DataGridViewCellEventHandler(dgvHienThi_CellDoubleClick);
        }

        #region xử lý Form
        void dgvHienThi_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            i = e.RowIndex; if (i < 0)
                return;
            try
            {
                Chon();
            }
            catch
            {
            }
        }

        void frmTimKiemKhachHang_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (!checkClose)
                if (MessageBox.Show(Common.MSG.MSG2(), Common.MSG.TieuDe, MessageBoxButtons.YesNo) == DialogResult.Yes)
                    e.Cancel = checkClose;
                else
                    e.Cancel = !checkClose;
        }
        int i = -1;
        void dgvHienThi_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
               // khachhang = (Entities.KhachHang)dgvHienThi.SelectedItem;

                i = e.RowIndex;
                for (int i1 = 0; i1 < hienthi.Length; i1++)
                {
                    if (hienthi[i1].MaKhachHang == dgvHienThi.CurrentRow.Cells["MaKhachHang"].Value.ToString())
                    {
                        khachhang = hienthi[i1];
                    }
                }

            }
            catch
            {
            }
        }
        // tìm kiếm
        void txtTimKiem_TextChanged(object sender, EventArgs e)
        {
            khachhang = null;
            ArrayList arr = new ArrayList();
            if (rdbTimKiem1.Checked == true)
            {

                foreach (Entities.KhachHang item in hienthi)
                {
                    if (item.MaKhachHang.ToUpper().IndexOf(txtTimKiem.Text.ToUpper()) >= 0)
                        arr.Add(item);
                }

            }
            if (rdbTimKiem2.Checked == true)
            {
                foreach (Entities.KhachHang item in hienthi)
                {
                    if (item.Ten.ToUpper().IndexOf(txtTimKiem.Text.ToUpper()) >= 0)
                        arr.Add(item);
                }
            }
            dgvHienThi.DataSource = (Entities.KhachHang[])arr.ToArray(typeof(Entities.KhachHang));
            //lbTongSo.Content = Common.Constants.TongSo + arr.Count;
        }
        #endregion

        private void MouseDown_Chung(object sender, MouseEventArgs e)
        {
            try
            {
                Button temp = (Button)sender;

                #region chuột trái
                
                    switch (temp.Name)
                    {
                        case "btnChon":
                            {
                                Chon();
                                break;
                            }
                        case "btnThoat":
                            {
                                Thoat();
                                break;
                            }
                        default:
                            break;
                    }
                #endregion
            }
            catch
            {
            }
        }

        private void KeyDown_Chung(object sender, KeyEventArgs e)
        {
            try
            {
                if (e.KeyCode == Keys.F2)
                {
                    Chon();
                }
                if (e.KeyCode == Keys.Escape)
                {
                    Thoat();
                }
                if (e.KeyCode == Keys.Enter)
                {
                    Chon();
                }
            }
            catch
            {
            }
        }


    }
}
